package hibernateDemo;



import java.util.List;
import java.util.Set;

import hibernateDemo.DAO.StudentDAO;
import hibernateDemo.DAO.TeacherDAO;
import hibernateDemo.entity.Student;
import hibernateDemo.entity.Teacher;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.MenuButton;
import javafx.scene.control.MenuItem;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;

public class HibernateController {

	 @FXML
	    private MenuItem dButton;

	    @FXML
	    private TextField studentTutorText;

	    @FXML
	    private TextField tutorNameColumn1;

	    @FXML
	    private TextField studentNameText;

	    @FXML
	    private TextArea studentArea;

	    @FXML
	    private MenuItem uButton;

	    @FXML
	    private TextField tutorIdText;

	    @FXML
	    private MenuItem rButton;

	    @FXML
	    private TextArea teacherArea;

	    @FXML
	    private MenuItem dButton2;

	    @FXML
	    private AnchorPane studentNOField;

	    @FXML
	    private MenuButton crudButton;

	    @FXML
	    private MenuItem rButton2;

	    @FXML
	    private MenuItem uButton2;

	    @FXML
	    private MenuItem cButton2;

	    @FXML
	    private TextArea studentArea2;

	    @FXML
	    private TextField tutorNameColumn;

	    @FXML
	    private MenuItem cButton;
	    
	    @FXML
	    private Button showButton;

	    @FXML
	    private TextField studentIdText;
    private static StudentDAO studentDAO;
    private static TeacherDAO teacherDAO;
    private Stage stage;
    static{
    	studentDAO=new StudentDAO();
    	teacherDAO=new TeacherDAO();
    	}
    public void begin(Stage primaryStage,Parent root) {
    	stage=primaryStage;
    	stage.setTitle("Hibernate");
    	stage.setScene(new Scene(root));
    	stage.show();
    }
    @FXML
    void student_c(ActionEvent event) {//create
    	studentArea2.clear();
    	Student student=new Student();
    	student.setId(Integer.parseInt(studentIdText.getText().trim()));
    	if((studentDAO.get(Integer.parseInt(studentIdText.getText())))!=null) {
    		studentArea2.clear();
			studentArea2.appendText("!!!!!!!!!!!!!!!!!!\n");
			studentArea2.appendText("已经有编号为"+Integer.valueOf(studentIdText.getText().trim())+"的学生，请重新输入\n");
			return;
    	}
    	student.setName(studentNameText.getText());
    	if(!studentTutorText.getText().trim().isEmpty()) {
    		String[] strings=studentTutorText.getText().trim().split(",");
    		Set teachers=student.getTeachers(); 
    		Teacher teacher;
    		for(int i=0;i<strings.length;i++) {
    			teacher=null;
    			if((teacher=teacherDAO.get(Integer.valueOf(strings[i])))!=null) {
    				teachers.add(teacher);
    			}
    			else {
    				studentArea2.clear();
    				studentArea2.appendText("!!!!!!!!!!!!!!!!!!\n");
    				studentArea2.appendText("没有编号为"+Integer.valueOf(strings[i])+"的老师，请重新输入\n");
    				return;
    			}
    		}
    	}
    	studentDAO.save(student);
    	show();

    }

    @FXML
    void student_d(ActionEvent event) {//delete
    	studentArea2.clear();
    	Student student=new Student();
    	student.setId(Integer.parseInt(studentIdText.getText().trim()));
    	student.setName(studentNameText.getText().trim());
    	studentDAO.delete(student);
    	show();
    }
    void show() {
    	studentArea.clear();
    	studentArea.appendText("学生学号\t"+"学生姓名\n");
    	List<Student> list=studentDAO.getAll();
    	list.forEach((s)->{
    		studentArea.appendText(s.getId()+"\t\t"+s.getName()+"\n");
    	});
    	teacherArea.clear();
    	teacherArea.appendText("教师编号\t"+"教师姓名\n");
    	List<Teacher> list2=teacherDAO.getAll();
    	list2.forEach((s)->{
    		teacherArea.appendText(s.getId()+"\t\t"+s.getName()+"\n");
    	});
    }
    @FXML
    void showmeAgain(ActionEvent event) {
    	
    	show();
    }
    
    
    @FXML
    void student_r(ActionEvent event) {//retrieve
    	studentArea2.clear();
    	Student student=null;
    	if((student=studentDAO.get(Integer.parseInt(studentIdText.getText())))!=null){
    		studentArea.clear();
    		studentArea.appendText("学生\n");
    		studentArea.appendText(student.getId()+"\t\t"+student.getName()+"\n");
    		Set<Teacher> t=student.getTeachers();
    		studentArea.appendText("老师\n");
    		t.forEach(s->{
    			studentArea.appendText(s.getId()+"\t\t"+s.getName()+"\n");
    		});
    	}
    	else {
    		studentArea2.appendText("!!!!!!!!!!!!!\n");
    		studentArea2.appendText("查无此生");
    	}
    }

    @FXML
    void student_u(ActionEvent event) {//update
    	studentArea2.clear();
    	Student student=new Student();
    	student.setId(Integer.parseInt(studentIdText.getText()));
    	student.setName(studentNameText.getText());
    	if(!studentTutorText.getText().trim().isEmpty()) {
    		String[] strings=studentTutorText.getText().trim().split(",");
    		Set teachers=student.getTeachers(); 
    		Teacher teacher;
    		for(int i=0;i<strings.length;i++) {
    			teacher=null;
    			if((teacher=teacherDAO.get(Integer.valueOf(strings[i])))!=null) {
    				teachers.add(teacher);
    			}
    			else {
    				studentArea2.appendText("!!!!!!!!!!!!!!!!!!\n");
    				studentArea2.appendText("没有编号为"+Integer.valueOf(strings[i])+"的老师，请重新输入\n");
    				return;
    			}
    		}
    	}
    	studentDAO.update(student);
    	show();

    }

    @FXML
    void teacher_c(ActionEvent event) {
    	studentArea2.clear();
    	Teacher teacher=new Teacher();
    	teacher.setId(Integer.parseInt(tutorIdText.getText().trim()));
    	if((teacherDAO.get(Integer.parseInt(tutorIdText.getText())))!=null) {
    		studentArea2.clear();
			studentArea2.appendText("!!!!!!!!!!!!!!!!!!\n");
			studentArea2.appendText("已经有编号为"+Integer.valueOf(tutorIdText.getText().trim())+"的老师，请重新输入\n");
			return;
    	}
    	teacher.setName(tutorNameColumn.getText());
    	if(!tutorNameColumn1.getText().trim().isEmpty()) {
    		String[] strings=tutorNameColumn1.getText().trim().split(",");
    		Set students=teacher.getStudents(); 
    		Student student;
    		for(int i=0;i<strings.length;i++) {
    			student=null;
    			if((student=studentDAO.get(Integer.valueOf(strings[i])))!=null) {
    				students.add(student);
    			}
    			else {
    				studentArea2.clear();
    				studentArea2.appendText("!!!!!!!!!!!!!!!!!!\n");
    				studentArea2.appendText("没有编号为"+Integer.valueOf(strings[i])+"的学生，请重新输入\n");
    				return;
    			}
    		}
    	}
    	teacherDAO.save(teacher);
    	show();

    }

    @FXML
    void teacher_d(ActionEvent event) {
    	studentArea2.clear();
    	Teacher teacher=new Teacher();
    	teacher.setId(Integer.parseInt(tutorIdText.getText()));
    	teacher.setName(tutorNameColumn.getText());
    	teacherDAO.delete(teacher);
    	show();
    }

    @FXML
    void teacher_r(ActionEvent event) {
    	studentArea2.clear();
    	Teacher teacher=null;
    	if((teacher=teacherDAO.get(Integer.parseInt(tutorIdText.getText())))!=null){
    		teacherArea.clear();
    		teacherArea.appendText("老师\n");
    		teacherArea.appendText(teacher.getId()+"\t\t"+teacher.getName()+"\n");
    		Set<Student> t=teacher.getStudents();
    		teacherArea.appendText("学生\n");
    		t.forEach(s->{
    			teacherArea.appendText(s.getId()+"\t\t"+s.getName()+"\n");
    		});
    	}
    	else {
    		studentArea2.appendText("!!!!!!!!!!!!!\n");
    		studentArea2.appendText("查无此老师");
    	}

    }

    @FXML
    void teacher_u(ActionEvent event) {
    	studentArea2.clear();
    	Teacher teacher=new Teacher();
    	teacher.setId(Integer.parseInt(tutorIdText.getText()));
    	teacher.setName(tutorNameColumn.getText());
    	if(!tutorNameColumn1.getText().trim().isEmpty()) {
    		String[] strings=tutorNameColumn1.getText().trim().split(",");
    		Set students=teacher.getStudents(); 
    		Student student;
    		for(int i=0;i<strings.length;i++) {
    			student=null;
    			if((student=studentDAO.get(Integer.valueOf(strings[i])))!=null) {
    				students.add(student);
    			}
    			else {
    				studentArea2.appendText("!!!!!!!!!!!!!!!!!!\n");
    				studentArea2.appendText("没有编号为"+Integer.valueOf(strings[i])+"的学生，请重新输入\n");
    				return;
    			}
    		}
    	}
    	teacherDAO.update(teacher);
    	show();
    }

}
